package top.shiyiri.exer2;

import org.junit.Test;

import java.util.*;

/**
 * @author Aunean
 * @date 2022/1/6
 */
public class Exercise {


    @Test
    public void test1() {
        //请从键盘随机输入10个整数保存到List中，并按倒序、从大到小的顺序显示出来
        List<Integer> list = new ArrayList<>();
        list.add(123);
        list.add(-23);
        list.add(223);
        list.add(213);
        list.add(63);
        list.add(28);
        list.add(62);
        list.add(73);
        System.out.println(list);

        Collections.reverse(list);
        System.out.println(list);

        Collections.sort(list);
        Collections.reverse(list);
        System.out.println(list);
    }

    @Test
    public void test2() {
        //请把学生名与考试分数录入到集合中，并按分数显示前三名成绩学员的名字。
        // TreeSet(Student(name,score));

        Student t1 = new Student("Tom", 23);
        Student t2 = new Student("Jerry", 63);
        Student t3 = new Student("Rose", 78);
        Student t4 = new Student("Anna", 21);
        Student t5 = new Student("Tomas", 56);

        TreeSet<Student> set = new TreeSet<>(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return (int) (o2.getScore() - o1.getScore());
            }
        });
        set.add(t1);
        set.add(t2);
        set.add(t3);
        set.add(t4);
        set.add(t5);

        Iterator<Student> iterator = set.iterator();
        int rank = 0;
        for (;iterator.hasNext();) {
            if (rank == 3) {
                break;
            }
            rank++;
            System.out.println(iterator.next());
        }
    }
}
